﻿using System;
using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class Bomb : BaseMono, IOnExplode
{
	public event Action OnBombExplode;

	public bool Enable;
	public const float DelayTime = 3;

	private float explodeDis;
	public AudioSource Audio;

	public float ExplodeDis
	{
		get { return explodeDis; }
		set
		{
			explodeDis = Mathf.Max(1, value);
		}
	}

	public BoxCollider2D BoxCollider2D;
	private Transform WallRoot;
	public Image[] fireEffects;

	public override void onEnable()
	{
		MainGame.AStarGridMgr.SetNodeCanMove(MyTrans.position, false);
	}

	public override void onDisable()
	{
		MainGame.AStarGridMgr.SetNodeCanMove(MyTrans.position, true);
	}

	public override void onAwake()
	{
		WallRoot = GameObject.FindWithTag("Root").transform;
		Audio = gameObject.TryGetComponent<AudioSource>();
		BoxCollider2D = GetComponent<BoxCollider2D>();
		BoxCollider2D.isTrigger = true;

		fireEffects = new Image[5];
		for (int i = 0; i < 5; i++)
		{
			GameObject go = null;
			if (i != 4)
			{
				go = Instantiate(GameRes.Ins.FireRight);
				RectTransform rectTrans = go.GetComponent<RectTransform>();
				rectTrans.pivot = new Vector2(0, 0.5f);
			}
			else
			{
				go = Instantiate(GameRes.Ins.FireMiddle);
			}
			go.transform.SetParent(WallRoot);
			Image img = go.GetComponent<Image>();
			img.color = Color.clear;
			go.TryGetComponent<BoxCollider2D>().isTrigger = true;

			fireEffects[i] = img;
		}

		Enable = true;
		base.Init();
	}

	public void BePlaced(int dis)
	{
		ExplodeDis = dis;
		StartCoroutine(DelayToDo(DelayTime, Explode));
	}

	public override void onUpdate()
	{
		CheckPlayerPos();
	}

	void CheckPlayerPos()
	{
		BoxCollider2D.isTrigger = Vector3.Distance(MainGame.Player.MyTrans.position, MyTrans.position) < 0.9f;
	}

	IEnumerator DelayToDo(float delay, Action act)
	{
		yield return new WaitForSeconds(delay);

		if (act != null)
		{
			act();
		}
	}

	public void OnExplode()
	{
		Explode();
	}

	void Explode()
	{
		if (!Enable)
		{
			return;
		}
		Enable = false;

		DetectByDirection(0, Vector2.up);
		DetectByDirection(1, Vector2.down);
		DetectByDirection(2, Vector2.left);
		DetectByDirection(3, Vector2.right);
		ShowFire(fireEffects[4],1.1f, Vector2.zero);
		Audio.PlayOneShot(SoundMgr.Ins.Boom);

		if (OnBombExplode != null)
		{
			OnBombExplode();
		}

		OnBombExplode = null;

		GetComponent<Image>().color = Color.clear;
		BoxCollider2D.enabled = false;
		Destroy(this.gameObject, 3);
	}

	void DetectByDirection(int index, Vector2 dir)
	{
		float dis = ExplodeDis;
		//Detect Wall
		RaycastHit2D hitInfo = Physics2D.Raycast(MyTrans.position, dir, ExplodeDis, 1 << LayerMask.NameToLayer("Wall"));

			if (hitInfo.collider && hitInfo.collider.gameObject)
			{
				if (hitInfo.collider.gameObject.layer.Equals(LayerMask.NameToLayer("Wall")))
				{
					dis = Mathf.Min(dis, Vector3.Distance(MyTrans.position, hitInfo.point) - 0.5f);
					IOnExplode explode = hitInfo.collider.gameObject.GetComponent<IOnExplode>();
					if (explode != null)
					{
						explode.OnExplode();
					}
				}
			}

		Image img = fireEffects[index];
		ShowFire(img, dis, dir);

//		Detect Characters And Prop
		RaycastHit2D[] hitInfos = Physics2D.CircleCastAll(MyTrans.position, 0.3f, dir, dis);
		for (int i = 0; i < hitInfos.Length; i++)
		{
			RaycastHit2D hitInfo2 = hitInfos[i];
			if (hitInfo2.collider && hitInfo2.collider.gameObject)
			{
				if (!hitInfo2.collider.gameObject.layer.Equals(LayerMask.NameToLayer("Wall")))
				{
					IOnExplode explode = hitInfo2.collider.gameObject.GetComponent<IOnExplode>();
					if (explode != null)
					{
						explode.OnExplode();
					}
				}
			}
		}
	}

	public void ShowFire(Image img, float dis, Vector2 dir)
	{
		img.color = Color.white;
		img.gameObject.TryGetComponent<FireEffect>().Fire();
		RectTransform rectTrans = img.GetComponent<RectTransform>();
		rectTrans.position = MyTrans.position + new Vector3(dir.x, dir.y ,MyTrans.position.z) * 0.5f;
		rectTrans.rotation = Quaternion.FromToRotation(Vector2.right, dir);
		rectTrans.sizeDelta = new Vector2(dis, rectTrans.sizeDelta.y + 0.1f);
	}
}
